﻿using System;
using Framework.Poco;
using Shell.DomainDot;
using System.Linq;

namespace Framework.Domain
{
    public class HotelInAgency : DomainDotBase<Agency>
    {
        public WithOne<Hotel> Hotel { private get; set; }

        public WithAll<HotelCreator, Hotel> HotelBelonged { private get; set; }

        public Hotel GetHotel()
        {
            return this.Hotel.Instance;
        }

        public void Create(Hotel hotel, Guid featuredWeddingIdentity, Guid churchWeddingIdentity)
        {
            if (this.GetHotel() == null)
            {
                hotel.Created = DateTime.Now;
                var instance = this.HotelBelonged.Create(hotel);
                this.Hotel.Bind(hotel.Id);
                this.Hotel.Submit();
                instance.BindChurchWedding(churchWeddingIdentity);
                instance.BindFeaturedWedding(featuredWeddingIdentity);
            }
            else
            {
                this.Edit(hotel, featuredWeddingIdentity, churchWeddingIdentity);
            }
        }

        public void Edit(Hotel hotel, Guid featuredWeddingIdentity, Guid churchWeddingIdentity)
        {
            var original = this.GetHotel();
            original.Cuisine = hotel.Cuisine;
            original.Lawn = hotel.Lawn;
            original.Outdoor = hotel.Outdoor;
            original.Parking = hotel.Parking;
            original.StarHotel = hotel.StarHotel;
            original.Style = hotel.Style;
            this.Hotel.Submit();
            var instance = DomainDotFactory.Factory.Create<HotelCreator>(original.Id);
            instance.ChurchWeddingRelease();
            instance.BindChurchWedding(churchWeddingIdentity);
            instance.FeaturedWeddingRelease();
            instance.BindFeaturedWedding(featuredWeddingIdentity);
        }
    }
}
